package com.library.service.serviceImpl;

import com.library.entity.Questions;
import com.library.mapper.QuestionsMapper;
import com.library.service.IQuestionsService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Service
public class QuestionsServiceImpl extends ServiceImpl<QuestionsMapper, Questions> implements IQuestionsService {

	@Autowired
	private QuestionsMapper questionsMapper;
	
	@Override
	public List<Questions> createPaper0(int lessonId) {
		List<Questions> questions =questionsMapper.selectList(new EntityWrapper<Questions>().
				eq("lessonId", lessonId).eq("type", 0).orderBy("rand() LIMIT 15"));
		return questions;
	}
	@Override
	public List<Questions> createPaper1(int lessonId) {
		List<Questions> questions =questionsMapper.selectList(new EntityWrapper<Questions>().
				eq("lessonId", lessonId).eq("type", 1).orderBy("rand() LIMIT 5"));
		return questions;
	}

	@Override
	public boolean deleteQuestion(int questionsId) {
		int flag=questionsMapper.delete(new EntityWrapper<Questions>().eq("questionsId", questionsId));
		if(flag==0)
			return false;
		return true;
	}

	@Override
	public boolean addQuestion(Questions questions) {
		int flag=questionsMapper.insert(questions);
		if(flag==0)
			return false;
		return true;	
	}

	@Override
	public boolean updateQuestion(Questions questions) {
		int flag=questionsMapper.updateById(questions);
		if(flag==0)return false;
		return true;
	}

	@Override
	public List<Questions> showPageQuestions(int current, int size, int lessonId) {
		return questionsMapper.selectPage(new RowBounds(current, size), new EntityWrapper<Questions>().eq("lessonId", lessonId));
	}

	@Override
	public int getCount(int lessonId, int readerId) {
		if(lessonId!=0)
			return questionsMapper.selectCount(new EntityWrapper<Questions>().eq("lessonId", lessonId));
		else 
			return questionsMapper.selectCount(new EntityWrapper<Questions>().eq("readerId", readerId));
	}
	

}
